﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SurveyHost.Support
{
    public static class ObjectExtensions
    {
        /// <summary>
        /// Returns the string representation of the current object if it is not null, 
        /// <paramref name="ifNull"/> if it is.
        /// </summary>
        /// <param name="me">The <c>object</c></param>
        /// <param name="ifNull">The text to return if the object is <c>null</c></param>
        /// <returns><paramref name="me"/>.ToString() if <paramref name="me"/> is not <c>null</c>, 
        /// <paramref name="ifNull"/> if <paramref name="me"/> is <c>null</c></returns>
        public static string ToStringOr(this object me, string ifNull)
        {
            return me == null ? ifNull : me.ToString();
        }

        /// <summary>
        /// Calls ToString on the object and Html encodes the result.
        /// </summary>
        /// <remarks>The result is the same as calling ToString() and HtmlEncoding the result.  A null object returns the empty string.</remarks>
        /// <param name="me">The object to convert and encode</param>
        /// <returns>An empty string if <paramref name="me"/> is null, or the Html encoded result of calling ToString on the object.</returns>
        public static string HtmlEncode(this object me)
        {
            if (me == null) return string.Empty;
            return me.ToString().HtmlEncode();
        }
        /// <summary>
        /// Calls ToString on the object and Html encodes the result, or returns <paramref name="ifNull"/> if the object is null.
        /// </summary>
        /// <remarks>The result is the same as calling ToString() and HtmlEncoding the result.  A null object returns the value of <paramref name="ifNull"/>, which is also Html encoded before being returned.</remarks>
        /// <param name="me">The object to convert and encode</param>
        /// <returns>An empty string if <paramref name="me"/> is null, or the Html encoded result of calling ToString on the object.</returns>
        public static string HtmlEncode(this object me, string ifNull)
        {
            if (me == null) return ifNull.HtmlEncode();
            return HtmlEncode(me);
        }

    }
}
